package com.valkyries.jhhj.utils;

import com.google.code.kaptcha.text.impl.DefaultTextCreator;

import java.util.Random;

/**
 * 验证码文本生成器
 *
 * @author ruoyi
 */
public class KaptchaTextCreator extends DefaultTextCreator {

    private static final String[] NUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(",");

    /**
     * 生成四则运算验证码
     */
    @Override
    public String getText() {
        int res;
        Random random = new Random();
        int x = random.nextInt(10);
        int y = random.nextInt(10);
        StringBuilder sb = new StringBuilder();
        int op = random.nextInt(3);
        if (op == 0) {
            res = x * y;
            sb.append(NUMBERS[x]).append("*").append(NUMBERS[y]);
        } else if (op == 1) {
            if ((x != 0) && y % x == 0) {
                res = y / x;
                sb.append(NUMBERS[y]).append("/").append(NUMBERS[x]);
            } else {
                res = x + y;
                sb.append(NUMBERS[x]).append("+").append(NUMBERS[y]);
            }
        } else {
            res = x - y < 0 ? y - x : x - y;
            if(x - y < 0){
                int t = x;
                x = y;
                y = t;
            }
            sb.append(NUMBERS[x]).append("-").append(NUMBERS[y]);
        }
        sb.append("=?@").append(res);
        return sb.toString();
    }
}
